﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Ha666.Common
{
    public class Utility
    {
        public static string DbNull(object Str)
        {
            string str = "";
            if (Str == DBNull.Value || Convert.ToString(Str) == "" || Str == null || Convert.ToString(Str) == string.Empty || Convert.ToString(Str) == " " || Convert.ToString(Str) == "　")
            {
                str = "";
            }
            else
            {
                str = Convert.ToString(Str);
            }
            if (!string.IsNullOrEmpty(str)) str = str.Trim();
            return str;
        }
        public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }
        public static int ConvertDateTimeInt(DateTime time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// 计算字符串的Md5值
        /// </summary>
        public static string Md5FromString(string msg)
        {
            MD5 md5 = MD5.Create();
            byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(msg));
            var result = new StringBuilder();
            foreach (byte t in bytes)
                result.Append(t.ToString("X2"));
            return result.ToString();
        }

        /// <summary>
        /// 压缩字符串
        /// </summary>
        /// <param name="text">源字符串</param>
        public static byte[] Zip(string text)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            MemoryStream ms = new MemoryStream();
            using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true))
            {
                zip.Write(buffer, 0, buffer.Length);
            }
            ms.Position = 0;
            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);
            byte[] gzBuffer = new byte[compressed.Length + 4];
            Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
            return gzBuffer;
        }

        /// <summary>
        /// 解压缩字节数组
        /// </summary>
        /// <param name="gzBuffer">源字节数组</param>
        public static string UnZip(byte[] gzBuffer)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
                byte[] buffer = new byte[msgLength];
                ms.Position = 0;
                using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
                {
                    zip.Read(buffer, 0, buffer.Length);
                }
                return Encoding.UTF8.GetString(buffer, 0, buffer.Length);
            }
        }

    }
}
